package per.dg.tools;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.mopub.common.MoPub;
import com.mopub.common.SdkConfiguration;
import com.mopub.common.SdkInitializationListener;
import com.mopub.mobileads.MoPubErrorCode;
import com.mopub.mobileads.MoPubInterstitial;
import com.mopub.mobileads.MoPubView;

import static androidx.constraintlayout.motion.utils.Oscillator.TAG;

public class MopubManager {

    public static MopubManager INSTANCE;

    private static final String BANNERID = "80df5262b6e54787a31cd384b3b92969";
    private static final String INTERSTITIALID = "42279e9f30304e7dbb7b8acbd28762f1";

    public static MopubManager getInstance(Context context){
        if(INSTANCE == null){
            synchronized (MopubManager.class){
                if(INSTANCE == null){
                    INSTANCE = new MopubManager(context);
                }
            }
        }
        return INSTANCE;
    }

    private Context context;

    private MoPubView moPubView;

    private MoPubInterstitial mInterstitial;

    private boolean canShow = false;

    private long showInterstitialTime = 0;

    public void setCanShow(boolean canShow) {
        this.canShow = canShow;
//        this.canShow = true;
        if(!canShow){
            if(moPubView != null)
            moPubView.destroy();
        }else {
            initBanner();
        }
    }

    private MopubManager(Context context){
        this.context = context;
        SdkConfiguration sdkConfiguration = new SdkConfiguration.Builder(BANNERID).build();
        MoPub.initializeSdk(this.context, sdkConfiguration, initSdkListener());
    }

    private SdkInitializationListener initSdkListener() {
        return new SdkInitializationListener() {
            @Override
            public void onInitializationFinished() {
                Log.e(TAG, "onInitializationFinished: ");
                if(moPubView != null){
                    initBanner();
                }
                initInterstitial();
            }
        };
    }

    public MopubManager setMoPubView(MoPubView moPubView) {
        this.moPubView = moPubView;
        initBanner();
        return this;
    }

    private void initBanner(){
        Log.e(TAG, "initBanner: ");
        if(canShow && moPubView != null){
            Log.e(TAG, "initBanner2: ");
            moPubView.setAdUnitId(BANNERID); // Enter your Ad Unit ID from www.mopub.com
            moPubView.setBannerAdListener(bannerAdListener);
//            moPubView.setAdSize(MoPubView.MoPubAdSize.HEIGHT_50); // Call this if you are not setting the ad size in XML or wish to use an ad size other than what has been set in the XML. Note that multiple calls to `setAdSize()` will override one another, and the MoPub SDK only considers the most recent one.
//            moPubView.loadAd(MoPubView.MoPubAdSize.HEIGHT_50); // Call this if you are not calling setAdSize() or setting the size in XML, or if you are using the ad size that has not already been set through either setAdSize() or in the XML
            moPubView.loadAd();

        }

    }

    MoPubView.BannerAdListener bannerAdListener = new MoPubView.BannerAdListener() {
        @Override
        public void onBannerLoaded(@NonNull MoPubView banner) {
            Log.e(TAG, "onBannerLoaded: " );
        }

        @Override
        public void onBannerFailed(MoPubView banner, MoPubErrorCode errorCode) {
            Log.e(TAG, "onBannerFailed: " );
//            initBanner();
        }

        @Override
        public void onBannerClicked(MoPubView banner) {
            Log.e(TAG, "onBannerClicked: " );
        }

        @Override
        public void onBannerExpanded(MoPubView banner) {
            Log.e(TAG, "onBannerExpanded: " );
        }

        @Override
        public void onBannerCollapsed(MoPubView banner) {
            Log.e(TAG, "onBannerCollapsed: " );
        }
    };

    private void initInterstitial(){
        mInterstitial = new MoPubInterstitial((Activity)context, INTERSTITIALID);
        mInterstitial.setInterstitialAdListener(interstitialAdListener);
        mInterstitial.load();
    }

    MoPubInterstitial.InterstitialAdListener interstitialAdListener = new MoPubInterstitial.InterstitialAdListener(){

        @Override
        public void onInterstitialLoaded(MoPubInterstitial interstitial) {
            Log.e(TAG, "onInterstitialLoaded: ");
        }

        @Override
        public void onInterstitialFailed(MoPubInterstitial interstitial, MoPubErrorCode errorCode) {
            Log.e(TAG, "onInterstitialFailed: ");
            initInterstitial();
        }

        @Override
        public void onInterstitialShown(MoPubInterstitial interstitial) {
            Log.e(TAG, "onInterstitialShown: ");
        }

        @Override
        public void onInterstitialClicked(MoPubInterstitial interstitial) {
            Log.e(TAG, "onInterstitialClicked: ");
        }

        @Override
        public void onInterstitialDismissed(MoPubInterstitial interstitial) {
            initInterstitial();
            Log.e(TAG, "onInterstitialDismissed: ");
        }
    };

    public void showInterstitial(){
        if(canShow){
            long l = System.currentTimeMillis();
            if(mInterstitial != null && mInterstitial.isReady() && (showInterstitialTime == 0 || l - showInterstitialTime > 60*1000)){
                mInterstitial.show();
                showInterstitialTime = l;
            }else {
                mInterstitial.load();
            }
        }
    }
}
